<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Models\System;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Info extends Model
{

    protected $table = 'users_info';

    protected $guarded = [];

    /**
     * 一对一关联
     * 用户信息与文件关联
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function file()
    {
        return $this->hasOne('App\Models\System\File', 'id', 'avatar');
    }

    public function provinceAll()
    {
        return $this->hasOne('App\Models\Common\Region', 'id', 'province');
    }

    public function cityAll()
    {
        return $this->hasOne('App\Models\Common\Region', 'id', 'city');
    }

    public function areaAll()
    {
        return $this->hasOne('App\Models\Common\Region', 'id', 'area');
    }

    static function UpdateModel( $uid, $data )
    {
        return self::where('uid', $uid)->update($data);
    }

    static function CreateModel( $data )
    {
        return self::create($data);
    }

    static function DeleteModel( $where )
    {
        return self::where($where)->delete();
    }

    static function ValueModel( $where = [], $value = '' )
    {
        return self::where($where)->value($value);
    }

    static function GroupModel( $group )
    {
        return self::select($group)->groupBy($group)->get();
    }

    static function CountModel( $where )
    {
        return self::where($where)->count();
    }

    static function Ranking()
    {
        return self::select(DB::raw('province as name, count(*) as value'))->groupBy('province')->get();
    }
}
